//
//  AnnouncerCollectionViewCell.m
//  MountEverestFM
//
//  Created by ChenNing on 15/11/2.
//  Copyright © 2015年 chenn. All rights reserved.
//

#import "AnnouncerCollectionViewCell.h"
#import "AnnouncerTableViewCell.h"

@interface AnnouncerCollectionViewCell()<UITableViewDataSource,UITableViewDelegate,AnnouncerTableViewCellDelegage>
//数据
@property(nonatomic,strong)NSArray *dataArray;
//tableView
@property(nonatomic,strong)UITableView *announcerTableView;
//页码
@property(nonatomic,assign)NSInteger page;
@property(nonatomic,assign)BOOL isRefresh;
@end

@implementation AnnouncerCollectionViewCell
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // 初始化tableview
        [self initTopTableView];
    }
    return self;
}
- (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes
{
    self.announcerTableView.frame = CGRectMake(0, 0, layoutAttributes.bounds.size.width, layoutAttributes.bounds.size.height);
}

/**
 *  初始化findTabelView
 */
- (void)initTopTableView
{
    self.announcerTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 0, 0) style:UITableViewStyleGrouped];
    self.announcerTableView.backgroundColor = UIColorFromRGB(0xe7e7e7);
    self.announcerTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.announcerTableView.dataSource = self;
    self.announcerTableView.delegate = self;
    [self.contentView addSubview:_announcerTableView];
    
    [self.announcerTableView registerClass:[AnnouncerTableViewCell class] forCellReuseIdentifier:@"announcerTBCell"];
    
    // 发送主播请求
    self.page = 1;
    [self setupRefresh];
}

// UITableViewDataSource 协议方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.dataArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return (FM_Screen_Width-FM_Screen_Pts_Width*40)/3+FM_Screen_Pts_Height*70;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return FM_Screen_Pts_Width*50;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return FM_Screen_Pts_Height*15;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIButton *titleView = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, FM_Screen_Width, FM_Screen_Pts_Width*50)];
    titleView.backgroundColor = [UIColor whiteColor];
    titleView.tag = section+10500;
    [titleView addTarget:self action:@selector(headerTouchUpAction:) forControlEvents:UIControlEventTouchUpInside];
    [titleView addTarget:self action:@selector(headerTouchDownAction:) forControlEvents:UIControlEventTouchDown];
    
    UIImageView *titleImageView = [[UIImageView alloc] initWithFrame:CGRectMake(FM_Screen_Pts_Width*10, (FM_Screen_Pts_Width*50-FM_Screen_Pts_Width*15)/2, FM_Screen_Pts_Width*15, FM_Screen_Pts_Width*15)];
    titleImageView.image = [UIImage imageNamed:@"findsection_logo@2x.png"];
    [titleView addSubview:titleImageView];
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(FM_Screen_Pts_Width*30, 0, FM_Screen_Pts_Width*100, FM_Screen_Pts_Width*50)];
    titleLabel.text = [[self.dataArray objectAtIndex:section] objectForKey:@"title"];
    titleLabel.font = [UIFont systemFontOfSize:15];
    [titleView addSubview:titleLabel];
    
    UILabel *moreLabel = [[UILabel alloc] initWithFrame:CGRectMake(FM_Screen_Width-FM_Screen_Pts_Width*5-FM_Screen_Pts_Height*28/4*3-FM_Screen_Pts_Width*100, 0, FM_Screen_Pts_Width*100, FM_Screen_Pts_Width*50)];
    moreLabel.text = @"更多";
    moreLabel.font = [UIFont systemFontOfSize:15];
    moreLabel.textAlignment = NSTextAlignmentRight;
    moreLabel.textColor = [UIColor grayColor];
    [titleView addSubview:moreLabel];
    
    UIImageView *moreImageView = [[UIImageView alloc] initWithFrame:CGRectMake(FM_Screen_Width-FM_Screen_Pts_Width*10-FM_Screen_Pts_Width*14/4*3, (FM_Screen_Pts_Width*50 - FM_Screen_Pts_Height*28/4*3)/2, FM_Screen_Pts_Width*14/4*3, FM_Screen_Pts_Height*28/4*3)];
    moreImageView.image = [UIImage imageNamed:@"cell_arrow_all@2x.png"];
    [titleView addSubview:moreImageView];
    
    return titleView;
}

// 松开时
- (void)headerTouchUpAction:(UIButton *)sender
{
    NSArray *btnSubViewsArr = sender.subviews;
    for (NSInteger i = 0; i < btnSubViewsArr.count; i++) {
        UIView *view = btnSubViewsArr[i];
        if ([view isKindOfClass:[UILabel class]]) {
            UILabel *label = (UILabel*)view;
            if (i == 1) {
                label.textColor = [UIColor blackColor];
            }else{
                label.textColor = [UIColor grayColor];
            }
        }
    }
    
    [self.delegate announcerMorePage:[[self.dataArray objectAtIndex:sender.tag-10500] objectForKey:@"name"] title:[[self.dataArray objectAtIndex:sender.tag-10500] objectForKey:@"title"]];
}
// 按下时
- (void)headerTouchDownAction:(UIButton *)sender
{
    NSArray *btnSubViewsArr = sender.subviews;
    for (UIView *view in btnSubViewsArr) {
        if ([view isKindOfClass:[UILabel class]]) {
            UILabel *label = (UILabel*)view;
            label.textColor = [UIColor orangeColor];
        }
    }
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    AnnouncerTableViewCell *announcerCell = [tableView dequeueReusableCellWithIdentifier:@"announcerTBCell"];
    announcerCell.selectionStyle = UITableViewCellSelectionStyleNone;
    NSArray *tempArr = [[self.dataArray objectAtIndex:indexPath.section] objectForKey:@"list"];
    announcerCell.announcerArray = tempArr;
    announcerCell.delegate = self;
    return announcerCell;
}

- (void)topPage:(NSDictionary *)topDic
{
    [self.delegate announcerPage:topDic];
}

/**
 *  网络请求
 */
- (void)getDataWithUrl
{
    if (!self.isRefresh) {
        // 加载更多
        self.page++;
    }else{
        // 刷新
        self.page=1;
    }
    NSString *url = [NSString stringWithFormat:@"http://mobile.ximalaya.com/m/explore_user_index?device=iPhone&page=%ld",self.page];
    [NetWorkManager GET:url isCaChe:YES resultData:^(id responseObject) {
        if (responseObject != nil) {
            if (self.page == 1) {
                self.dataArray = [NSMutableArray array];
            }
            NSArray *tempArr = [responseObject objectForKey:@"list"];
            self.dataArray = [self.dataArray arrayByAddingObjectsFromArray:tempArr];
            [self.announcerTableView reloadData];
            [self setupEndRefresh];
        }
        // 移除加载更多
        if ([[responseObject objectForKey:@"maxPageId"] integerValue] == self.page) {
            self.announcerTableView.footer = nil;
        }else{
            // 尾
            self.announcerTableView.footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
                self.isRefresh = NO;
                [self getDataWithUrl];
            }];
        }
    } resultRrror:^(NSError *error) {
        NSLog(@"主播:error,%@",error);
    }];
}

// 设置刷新
- (void)setupRefresh
{
    // 头
    self.announcerTableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.isRefresh = YES;
        [self getDataWithUrl];
    }];
    [self.announcerTableView.header beginRefreshing];
    
    // 尾
    self.announcerTableView.footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        self.isRefresh = NO;
        [self getDataWithUrl];
    }];
}

// 结束刷新
- (void)setupEndRefresh
{
    [self.announcerTableView.header endRefreshing];
    [self.announcerTableView.footer endRefreshing];
}

@end
